/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ReplayControlForm.java
 *
 * Created on Oct 20, 2009, 9:25:22 AM
 */
package give.replay.gui;

import give.comm.Message;
import give.comm.StatusMessage;
import give.comm.EventSourceMessage;
import give.replay.GameEvent;
import give.replay.GameEventReplayer;
import java.awt.event.MouseEvent;
import java.util.Hashtable;
import java.util.List;
import javax.swing.JLabel;

/**
 *
 * @author koller
 */
public class ReplayControlForm extends javax.swing.JFrame {
    private GameEventReplayer replayer;
    private EventListModel model;
    private AbstractMain main;

    /** Creates new form ReplayControlForm */
    public ReplayControlForm(GameEventReplayer replayer, AbstractMain main) {
        setLocationByPlatform(true);
        this.replayer = replayer;
        this.main = main;
        initComponents();

        populateEventList();
        initSpeedSlider();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        playPauseButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        speedSlider = new javax.swing.JSlider();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        eventList = new javax.swing.JList();
        jumpToEventButton = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        openFileMenu = new javax.swing.JMenuItem();
        quitFileMenu = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("GIVE-2 Map Replay");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Playback controls"));

        playPauseButton.setText("||");
        playPauseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                playPauseButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Playback speed:");

        speedSlider.setMajorTickSpacing(1);
        speedSlider.setMaximum(9);
        speedSlider.setMinimum(-9);
        speedSlider.setPaintLabels(true);
        speedSlider.setPaintTicks(true);
        speedSlider.setSnapToTicks(true);
        speedSlider.setValue(0);

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(playPauseButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 52, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(speedSlider, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                .add(playPauseButton)
                .add(jLabel1))
            .add(speedSlider, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Game events"));

        eventList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                eventListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(eventList);

        jumpToEventButton.setText("Jump to event");
        jumpToEventButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jumpToEventButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 478, Short.MAX_VALUE)
                    .add(jumpToEventButton))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jumpToEventButton)
                .addContainerGap())
        );

        jMenu1.setText("File");

        openFileMenu.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.META_MASK));
        openFileMenu.setText("Open database ...");
        openFileMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openFileMenuActionPerformed(evt);
            }
        });
        jMenu1.add(openFileMenu);

        quitFileMenu.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.META_MASK));
        quitFileMenu.setText("Quit");
        quitFileMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitFileMenuActionPerformed(evt);
            }
        });
        jMenu1.add(quitFileMenu);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void playPauseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_playPauseButtonActionPerformed
        if (replayer.getState() == GameEventReplayer.State.PLAYING) {
            setPlayPauseButtonState(GameEventReplayer.State.PAUSED);
            replayer.pause();
            jumpToEventButton.setEnabled(true);
        } else if (replayer.getState() == GameEventReplayer.State.PAUSED) {
            setPlayPauseButtonState(GameEventReplayer.State.PLAYING);
            replayer.resume();
            jumpToEventButton.setEnabled(false);
        }
    }//GEN-LAST:event_playPauseButtonActionPerformed

    public void setPlayPauseButtonState(GameEventReplayer.State state) {
        if (state == GameEventReplayer.State.PAUSED) {
            playPauseButton.setText(">");
            jumpToEventButton.setEnabled(true);
        } else {
            playPauseButton.setText("||");
            jumpToEventButton.setEnabled(false);
        }
    }

    private void jumpToEventButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jumpToEventButtonActionPerformed
        // CAUTION: don't use evt -- this method gets called with a null argument
        int row = eventList.getSelectedIndex();

        if (row >= 0) {
            main.jumpTo(model.getEventIndex(eventList.getSelectedIndex()));
        }
    }//GEN-LAST:event_jumpToEventButtonActionPerformed

    private void openFileMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openFileMenuActionPerformed
        main.openDbSelectionInterface();
    }//GEN-LAST:event_openFileMenuActionPerformed

    private void quitFileMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quitFileMenuActionPerformed
        System.exit(0);
    }//GEN-LAST:event_quitFileMenuActionPerformed

    private void eventListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_eventListMouseClicked
        if (evt.getClickCount() == 2) {
            jumpToEventButtonActionPerformed(null);
        }
    }//GEN-LAST:event_eventListMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList eventList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jumpToEventButton;
    private javax.swing.JMenuItem openFileMenu;
    private javax.swing.JButton playPauseButton;
    private javax.swing.JMenuItem quitFileMenu;
    private javax.swing.JSlider speedSlider;
    // End of variables declaration//GEN-END:variables

    public void populateEventList() {
        model = new EventListModel();
        List<GameEvent> events = replayer.getEvents();

        for (int i = 0; i < events.size(); i++) {
            Message m = events.get(i).message;

            if ( EventListCellRenderer.canRenderMessageOfThisType(m) ) {
                model.add(i, events.get(i));
            }
        }

        eventList.setModel(model);
        eventList.setCellRenderer(new EventListCellRenderer());
        eventList.repaint();
    }

    private void initSpeedSlider() {
        Hashtable labelTable = new Hashtable();
        labelTable.put(new Integer(0), new JLabel("x1"));
        labelTable.put(new Integer(9), new JLabel("x10"));
        labelTable.put(new Integer(-9), new JLabel("x 1/10"));
        speedSlider.setLabelTable(labelTable);

        JSliderAnnotator jsa = new JSliderAnnotator(speedSlider) {
            @Override
            protected String getAnnotation(int sliderValue) {
                if (sliderValue < 0) {
                    return "1/" + (-sliderValue + 1);
                } else {
                    return Integer.toString(sliderValue + 1);
                }
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                super.mouseReleased(e);

                int val = speedSlider.getValue();
                if (val < 0) {
                    ReplayControlForm.this.replayer.setSpeedfactor(1.0 / (-val + 1));
                } else {
                    ReplayControlForm.this.replayer.setSpeedfactor(val + 1);
                }
            }
        };

        speedSlider.addMouseListener(jsa);
        speedSlider.addMouseMotionListener(jsa);

    }
}
